home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / master / Examples / Shared_Lib / funcs.c < prev    next >
C/C++ Source or Header  |  1994-02-01  |  1KB  |  77 lines

  1.  
  2. /*
  3.  *  FUNCS.C
  4.  */
  5.  
  6. #include "defs.h"
  7.  
  8. /*
  9.  *  library interface
  10.  */
  11.  
  12. Prototype LibCall void LockTestLib(void);
  13. Prototype LibCall void UnLockTestLib(void);
  14. Prototype LibCall void PostString(__A0 const char *);
  15. Prototype LibCall long GetString(__A0 char *, __D0 long);
  16.  
  17. /*
  18.  *  library local
  19.  */
  20.  
  21. LibCall void
  22. LockTestLib(void)
  23. {
  24.     ObtainSemaphore(&SemLock);
  25. }
  26.  
  27. LibCall void
  28. UnLockTestLib(void)
  29. {
  30.     ReleaseSemaphore(&SemLock);
  31. }
  32.  
  33. LibCall void
  34. PostString(name)
  35. __A0 const char *name;
  36. {
  37.     Node *node;
  38.  
  39.     ObtainSemaphore(&SemLock);
  40.     node = AllocMem(sizeof(Node) + strlen(name) + 1, MEMF_PUBLIC);
  41.     node->ln_Name = (char *)(node + 1);
  42.     strcpy(node->ln_Name, name);
  43.     AddTail(&StrList, node);
  44.     ReleaseSemaphore(&SemLock);
  45. }
  46.  
  47. /*
  48.  *  returns actual length of returned string regardless of buffer size,
  49.  *  but only copies max chars to the buffer (including \0 which may
  50.  *  cut off part of the string if the string is too large to fit)
  51.  */
  52.  
  53. LibCall long
  54. GetString(buf, max)
  55. __A0 char *buf;
  56. __D0 long max;
  57. {
  58.     Node *node;
  59.     long len;
  60.  
  61.     ObtainSemaphore(&SemLock);
  62.     if (node = RemHead(&StrList)) {
  63.     len = strlen(node->ln_Name);
  64.     strncpy(buf, node->ln_Name, max);
  65.     if (len >= max)
  66.         buf[max-1] = 0;
  67.     FreeMem(node, sizeof(Node) + len + 1);
  68.     } else {
  69.     len = -1;
  70.     if (max > 0)
  71.         buf[0] = 0;
  72.     }
  73.     ReleaseSemaphore(&SemLock);
  74.     return (len);
  75. }
  76.  
  77.